﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;
using Casanova.Web;
using Domain.DAL;
using System.Data;
using Telerik.Web.UI;

namespace WebApp.RepairingTickets
{
    public partial class TicketDetails : System.Web.UI.Page
    {
        #region Private Properties

        public long RepairingTicketID
        {
            get
            {
                if (this.ViewState["RepairingTicketID"] != null)
                {
                    return Convert.ToInt64(this.ViewState["RepairingTicketID"]);
                }
                return MyConfig.NullNumber;
            }
            set
            {
                this.ViewState["RepairingTicketID"] = value.ToString();
            }
        }

        #endregion

        #region RadGrid's Event Handlers

        protected void grdResult_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                Utility.TranslateGridPagerItem(e);
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void grdResult_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
                {
                    GridEditableItem editableItem = (GridEditableItem)e.Item;
                    if (editableItem != null)
                    {
                        //Format cost
                        string temp = editableItem["Cost"].Text.Trim();
                        decimal cost;
                        if (!string.IsNullOrEmpty(temp) && temp != "&nbsp;")
                        {
                            if (decimal.TryParse(temp, out cost))
                            {
                                editableItem["Cost"].Text = cost.ToString(MyConfig.CurrencyFormat);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void grdResult_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            try
            {
                BindGrid();
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    GetParam();
                    if (this.RepairingTicketID != MyConfig.NullNumber)
                    {
                        LoadForm();
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            string url = "~/ReceivingTicket/ReceivingTicketManagement.aspx";

            if (!string.IsNullOrEmpty(QueryStringUtility.GetString("isReportByProductType")))
            {
                url = String.Format("{0}?productTypeID={1}&reportDateFrom={2}&reportDateTo={3}&preUrl={4}", this.ResolveUrl("~/ReportsAndStatistics/ReportByProductTypesDetails.aspx"), QueryStringUtility.GetString("productTypeID", MyConfig.NullNumber.ToString()), QueryStringUtility.GetString("reportDateFrom"), QueryStringUtility.GetString("reportDateTo"), QueryStringUtility.GetString("preUrl"));
            }
            else
            {
                url = this.ResolveUrl(QueryStringUtility.GetString("preUrl", url));
            }
            Response.Redirect(url);
        }

        #endregion

        #region Private Methods

        private void LoadForm()
        {
            try
            {
                RepairingTicketDAO rtDAO = RepairingTicketDAO.GetInstance();
                DataSet ds = new DataSet();
                ds = rtDAO.GetForDetail(this.RepairingTicketID);

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["FullName"] != null && ds.Tables[0].Rows[0]["FullName"] != DBNull.Value)
                    {
                        lblFullName.Text = ds.Tables[0].Rows[0]["FullName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"] != DBNull.Value)
                    {
                        lblMobile.Text = ds.Tables[0].Rows[0]["Mobile"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Address"] != null && ds.Tables[0].Rows[0]["Address"] != DBNull.Value)
                    {
                        lblAddress.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["ProductTypeName"] != null && ds.Tables[0].Rows[0]["ProductTypeName"] != DBNull.Value)
                    {
                        lblProductTypeName.Text = ds.Tables[0].Rows[0]["ProductTypeName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Manufacturer"] != null && ds.Tables[0].Rows[0]["Manufacturer"] != DBNull.Value)
                    {
                        lblManufacturer.Text = ds.Tables[0].Rows[0]["Manufacturer"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Model"] != null && ds.Tables[0].Rows[0]["Model"] != DBNull.Value)
                    {
                        lblModel.Text = ds.Tables[0].Rows[0]["Model"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["SerialNumber"] != null && ds.Tables[0].Rows[0]["SerialNumber"] != DBNull.Value)
                    {
                        lblSerialNumber.Text = ds.Tables[0].Rows[0]["SerialNumber"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["ProductDescription"] != null && ds.Tables[0].Rows[0]["ProductDescription"] != DBNull.Value)
                    {
                        lblProductDescription.Text = ds.Tables[0].Rows[0]["ProductDescription"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Note"] != null && ds.Tables[0].Rows[0]["Note"] != DBNull.Value)
                    {
                        lblNote.Text = ds.Tables[0].Rows[0]["Note"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["WarrantyConfirmDate"] != null && ds.Tables[0].Rows[0]["WarrantyConfirmDate"] != DBNull.Value)
                    {
                        lblWarrantyConfirmDate.Text = ds.Tables[0].Rows[0]["WarrantyConfirmDate"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["WarrantyConfirmer"] != null && ds.Tables[0].Rows[0]["WarrantyConfirmer"] != DBNull.Value)
                    {
                        lblWarrantyConfirmer.Text = ds.Tables[0].Rows[0]["WarrantyConfirmer"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsValidWarranty"] != null && ds.Tables[0].Rows[0]["IsValidWarranty"] != DBNull.Value)
                    {
                        lblIsValidWarranty.Text = ds.Tables[0].Rows[0]["IsValidWarranty"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityConfirmDate"] != null && ds.Tables[0].Rows[0]["RepairQualityConfirmDate"] != DBNull.Value)
                    {
                        lblRepairQualityConfirmDate.Text = ds.Tables[0].Rows[0]["RepairQualityConfirmDate"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityConfirmer"] != null && ds.Tables[0].Rows[0]["RepairQualityConfirmer"] != DBNull.Value)
                    {
                        lblRepairQualityConfirmer.Text = ds.Tables[0].Rows[0]["RepairQualityConfirmer"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityName"] != null && ds.Tables[0].Rows[0]["RepairQualityName"] != DBNull.Value)
                    {
                        lblRepairQualityName.Text = ds.Tables[0].Rows[0]["RepairQualityName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityNote"] != null && ds.Tables[0].Rows[0]["RepairQualityNote"] != DBNull.Value)
                    {
                        lblRepairQualityNote.Text = ds.Tables[0].Rows[0]["RepairQualityNote"].ToString();
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        private void BindGrid()
        {
            try
            {
                RepairingTicketDAO rtDAO = RepairingTicketDAO.GetInstance();
                DataSet ds = new DataSet();
                ds = rtDAO.ListAllHistories(this.RepairingTicketID);
                
                grdResult.DataSource = ds;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void GetParam()
        {
            this.RepairingTicketID = QueryStringUtility.GetInt64("RepairingTicketID", MyConfig.NullNumber);
        }

        #endregion

    }
}